package com.ruoyi.buy.service.impl;

import com.ruoyi.buy.domain.entity.PO.*;
import com.ruoyi.buy.domain.entity.buy0003.Buy0003AddInvAndItem;
import com.ruoyi.buy.domain.entity.buy0003.Buy0003AddPostAndItem;
import com.ruoyi.buy.mapper.PO.*;
import com.ruoyi.buy.service.IBuyPurchasePostService;
import com.ruoyi.buy.service.ICommonService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 采购邮寄Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-12-07
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BuyPurchasePostServiceImpl implements IBuyPurchasePostService
{
    @Resource
    private BuyPurchasePostMapper buyPurchasePostMapper;

    @Resource
    private ICommonService commonService;

    @Resource
    private BuyPurchasePostItemMapper buyPurchasePostItemMapper;

    @Resource
    private BuyPurchaseInvoiceMapper buyPurchaseInvoiceMapper ;

    @Resource
    private BuyPurchaseInvItemMapper buyPurchaseInvItemMapper ;

    @Resource
    private BuyPurchaseBillItemMapper buyPurchaseBillItemMapper ;



    /**
     * 查询采购邮寄
     * 
     * @param id 采购邮寄主键
     * @return 采购邮寄
     */
    @Override
    public BuyPurchasePost selectBuyPurchasePostById(Long id)
    {
        return buyPurchasePostMapper.selectBuyPurchasePostById(id);
    }

    /**
     * 查询采购邮寄列表
     * 
     * @param buyPurchasePost 采购邮寄
     * @return 采购邮寄
     */
    @Override
    public List<BuyPurchasePost> selectBuyPurchasePostList(BuyPurchasePost buyPurchasePost)
    {
        return buyPurchasePostMapper.selectBuyPurchasePostList(buyPurchasePost);
    }

    /**
     * 新增采购邮寄
     * 
     * @param buyPurchasePost 采购邮寄
     * @return 结果
     */
    @Override
    public int insertBuyPurchasePost(BuyPurchasePost buyPurchasePost)
    {
        String vBillNo = this.commonService.getNo("DH");
        buyPurchasePost.setvBillNo(vBillNo);
        buyPurchasePost.setCreateTime(DateUtils.getNowDate());
        return buyPurchasePostMapper.insertBuyPurchasePost(buyPurchasePost);
    }

    /**
     * 修改采购邮寄
     * 
     * @param buyPurchasePost 采购邮寄
     * @return 结果
     */
    @Override
    public int updateBuyPurchasePost(BuyPurchasePost buyPurchasePost)
    {
        buyPurchasePost.setUpdateTime(DateUtils.getNowDate());
        return buyPurchasePostMapper.updateBuyPurchasePost(buyPurchasePost);
    }

    /**
     * 批量删除采购邮寄
     * 
     * @param ids 需要删除的采购邮寄主键
     * @return 结果
     */
    @Override
    public int deleteBuyPurchasePostByIds(Long[] ids)
    {
        return buyPurchasePostMapper.deleteBuyPurchasePostByIds(ids);
    }

    /**
     * 删除采购邮寄信息
     * 
     * @param id 采购邮寄主键
     * @return 结果
     */
    @Override
    public int deleteBuyPurchasePostById(Long id)
    {
        return buyPurchasePostMapper.deleteBuyPurchasePostById(id);
    }

    @Override
    public int savePostWithItem(Buy0003AddPostAndItem para) {

        //邮寄主表
        BuyPurchasePost buyPurchasePost = para.getBuyPurchasePost();
        Date createTime = DateUtils.getNowDate();
        String vBillNo = this.commonService.getNo("DH");
        buyPurchasePost.setvBillNo(vBillNo);
        buyPurchasePost.setCreateTime(createTime);
        buyPurchasePostMapper.insertBuyPurchasePost(buyPurchasePost);

        //反查主表ID
        buyPurchasePost.setCreateTime(null);
        List<BuyPurchasePost> purchasePosts = buyPurchasePostMapper.selectBuyPurchasePostList(buyPurchasePost);
        Long nMainId = purchasePosts.get(0).getId();

        //子表数据
        List<BuyPurchasePostItem> buyPurchasePostItems = para.getBuyPurchasePostItems();
        for (BuyPurchasePostItem item:buyPurchasePostItems
             ) {
            item.setnMainId(nMainId);
            item.setnQty(item.getnThisQty());
            item.setCreateTime(createTime);
            item.setvProjectNo(buyPurchasePost.getvProjectNo());
            buyPurchasePostItemMapper.insertBuyPurchasePostItem(item);

            //更新已发物流数据
            BuyPurchaseBillItem buyPurchaseBillItem = new BuyPurchaseBillItem();
            buyPurchaseBillItem.setnMainId(buyPurchasePost.getnMainId());
            buyPurchaseBillItem.setvSpaName(item.getvSpaName());
            buyPurchaseBillItem.setvSpaBrand(item.getvSpaBrand());
            buyPurchaseBillItem.setvSpaName(item.getvSpaName());
            List<BuyPurchaseBillItem> billItemList = this.buyPurchaseBillItemMapper.selectBuyPurchaseBillItemList(buyPurchaseBillItem);
            buyPurchaseBillItem = billItemList.get(0);
            buyPurchaseBillItem.setnPostQty(buyPurchaseBillItem.getnPostQty()==null? BigDecimal.ZERO.add(item.getnQty()):
                    buyPurchaseBillItem.getnPostQty().add(item.getnQty())     );
            this.buyPurchaseBillItemMapper.updateBuyPurchaseBillItem(buyPurchaseBillItem);
        }


        return 1;
    }

    @Override
    public int saveInvWithItem(Buy0003AddInvAndItem para) {
        //发票主表
        BuyPurchaseInvoice buyPurchaseInvoice = para.getBuyPurchaseInvoice();
        Date createTime = DateUtils.getNowDate();
        buyPurchaseInvoice.setCreateTime(createTime);
        buyPurchaseInvoice.setCreateBy(commonService.getUsername());
        buyPurchaseInvoiceMapper.insertBuyPurchaseInvoice(buyPurchaseInvoice);

        //反查主表ID
        buyPurchaseInvoice.setCreateTime(null);
        List<BuyPurchaseInvoice> invoiceList = buyPurchaseInvoiceMapper.selectBuyPurchaseInvoiceList(buyPurchaseInvoice);
        Long nMainId = invoiceList.get(0).getId();

        //子表数据
        List<BuyPurchaseInvItem> buyPurchaseInvItems = para.getBuyPurchaseInvItems();
        for (BuyPurchaseInvItem item:buyPurchaseInvItems
        ) {
            item.setnMainId(nMainId);
            item.setvBillNo(buyPurchaseInvoice.getvBillNo());
            item.setnQty(item.getnThisQty());
            item.setCreateTime(createTime);
            item.setnPrice(item.getnPrice());
            item.setCreateBy(commonService.getUsername());
            item.setvProjectNo(buyPurchaseInvoice.getvProjectNo());
            buyPurchaseInvItemMapper.insertBuyPurchaseInvItem(item);

            //更新已开发票数据
            BuyPurchaseBillItem buyPurchaseBillItem = new BuyPurchaseBillItem();
            buyPurchaseBillItem.setnMainId(buyPurchaseInvoice.getnMainId());
            buyPurchaseBillItem.setvSpaName(item.getvSpaName());
            buyPurchaseBillItem.setvSpaBrand(item.getvSpaBrand());
            buyPurchaseBillItem.setvSpaName(item.getvSpaName());
            buyPurchaseBillItem.setUpdateTime(createTime);
            buyPurchaseBillItem.setUpdateBy(commonService.getUsername());
            List<BuyPurchaseBillItem> billItemList = this.buyPurchaseBillItemMapper.selectBuyPurchaseBillItemList(buyPurchaseBillItem);
            buyPurchaseBillItem = billItemList.get(0);
            buyPurchaseBillItem.setnInvQty(buyPurchaseBillItem.getnInvQty()==null? BigDecimal.ZERO.add(item.getnQty()):
                    buyPurchaseBillItem.getnInvQty().add(item.getnQty())     );
            this.buyPurchaseBillItemMapper.updateBuyPurchaseBillItem(buyPurchaseBillItem);
        }
        return 1;
    }
}
